////////////////////////////////////////////////////////////////////////////////
//
//  MICROSOFT CORPORATION
//  Copyright (c) Microsoft Corporation.
//  All Rights Reserved.
//
//  NOTICE: Microsoft Confidential. Intended for Internal Use Only.
//
////////////////////////////////////////////////////////////////////////////////

package com.msn.beet.logging
{

import com.adobe.errors.IllegalStateError;

import flash.external.ExternalInterface;

/**
 * Sends output from the Log utility to the Firebug Console. Script access must
 * be enabled or constructor will throw error.
 *
 * Specify a different pattern format with the following tokens:
 * {message}, {logLevel}, {upTime}, {timeStamp}, {className}, {line}
 *
 */
public class FirebugHandler extends TraceHandler implements ILogHandler
{
    include "../core/Version.as";

    public static const DEFAULT_FORMAT:String =
        "***  {logLevel}  IN  \"{className}\"  ON  line:{line}  " +
        "AT  {timeStamp}  ***"

    /**
     * Default header format layout:
     * <code>***  {logLevel}  IN  \"{className}\"  ON  line:{line} AT
     * {timeStamp}  ***</code>
     */
    protected var _headerFormat:String = DEFAULT_FORMAT;

    protected static const CONSOLE_GROUP:String     = "console.group";
    protected static const CONSOLE_GROUP_END:String = "console.groupEnd";
    protected static const CONSOLE_DEBUG:String     = "console.debug";
    protected static const CONSOLE_INFO:String      = "console.info";
    protected static const CONSOLE_WARN:String      = "console.warn";
    protected static const CONSOLE_ERROR:String     = "console.error";

    /**
     * @param headerPattern Passing "" will turn off collapsable headers in
     * Firebug console.
     * Overrides default: "***  {logLevel}  IN  \"{className}\"  ON
     * line:{line}  AT  {timeStamp}  ***"
     *
     * @param contentPattern overrides default: "{message}"
     * @throws Error if script access is disabled.
     */
    public function FirebugHandler(headerFormat:String = null,
                                   messageFormat:String = null)
    {
        super._format = messageFormat || "{message}";

        if (headerFormat != null)
        {
            _headerFormat = headerFormat;
        }

        if (!ExternalInterface.available)
        {
            throw new IllegalStateError("Script access disabled. " +
                                        "FirebugHandler cannot call Firebug " +
                                        "console.");
        }
    }

    /**
    * The current header layout, "" will remove header functionality from
    * firebug.
    */
    public function get headerFormat():String
    {
        return _headerFormat;
    }

    public function set headerFormat(value:String):void
    {
        _headerFormat = value;
    }

    /**
     * Writes message to Firebug console using Firebug's javascript API's.
     * @param message
     */
    override public function notify(message:Message):void
    {
        var level:int = message.logLevel.value;
        var consoleCmd:String;

        consoleCmd = (0 == level) ? CONSOLE_DEBUG : consoleCmd;
        consoleCmd = (1 == level) ? CONSOLE_INFO  : consoleCmd;
        consoleCmd = (2 == level) ? CONSOLE_WARN  : consoleCmd;
        consoleCmd = (level >= 3) ? CONSOLE_ERROR : consoleCmd;

        if (_headerFormat != "")
        {
            ExternalInterface.call(CONSOLE_GROUP,
                                   super.hydrateLayout(_headerFormat, message))
        }

        ExternalInterface.call(consoleCmd,
                               super.hydrateLayout(super._format, message))

        if (_headerFormat != "")
        {
            ExternalInterface.call(CONSOLE_GROUP_END);
        }
    }

}

}
